package org.huangrui.juc.source.cf;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * 谁快谁是赢家
 * @Author hr
 * @Create 2024-04-01 13:58
 */
public class T10_CompletableFutureFast {
    public static void main(String[] args) {
        CompletableFuture<String> playA = CompletableFuture.supplyAsync(() -> {
            System.out.println("A come in");
            try {
                TimeUnit.MILLISECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "play A";
        });

        CompletableFuture<String> playB = CompletableFuture.supplyAsync(() -> {
            System.out.println("B come in");
            try {
                TimeUnit.MILLISECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "play B";
        });

        CompletableFuture<String> result = playB.applyToEither(playA, f -> f + " is winer");
        System.out.println(Thread.currentThread().getName() + "\t -----come in " + result.join());
    }
}
